<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Fixtures</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Fixtures</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/vendor/rails/activerecord/lib/active_record/fixtures_rb.html">
                vendor/rails/activerecord/lib/active_record/fixtures.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                (RUBY_VERSION &lt; '1.9' ? YAML::Omap : Hash)
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
<a href="Fixtures.html">Fixtures</a> are a way of organizing data that you
want to test against; in short, sample data. They come in 3 flavors:
</p>
<pre>
  1.  YAML fixtures
  2.  CSV fixtures
  3.  Single-file fixtures
</pre>
<h1>YAML fixtures</h1>
<p>
This type of fixture is in YAML format and the preferred default. YAML is a
file format which describes data structures in a non-verbose,
human-readable format. It ships with Ruby 1.8.1+.
</p>
<p>
Unlike single-file fixtures, YAML fixtures are stored in a single file per
model, which are placed in the directory appointed by
<tt>ActiveSupport::TestCase.fixture_path=(path)</tt> (this is automatically
configured for <a href="Rails.html">Rails</a>, so you can just put your
files in <tt>&lt;your-rails-app&gt;/test/fixtures/</tt>). The fixture file
ends with the <tt>.yml</tt> file extension (<a href="Rails.html">Rails</a>
example: <tt>&lt;your-rails-app&gt;/test/fixtures/web_sites.yml</tt>). The
format of a YAML fixture file looks like this:
</p>
<pre>
  rubyonrails:
    id: 1
    name: Ruby on Rails
    url: http://www.rubyonrails.org

  google:
    id: 2
    name: Google
    url: http://www.google.com
</pre>
<p>
This YAML fixture file includes two fixtures. Each YAML fixture (ie.
record) is given a name and is followed by an indented list of key/value
pairs in the &quot;key: value&quot; format. Records are separated by a
blank line for your viewing pleasure.
</p>
<p>
Note that YAML fixtures are unordered. If you want ordered fixtures, use
the omap YAML type. See <a
href="http://yaml.org/type/omap.html">yaml.org/type/omap.html</a> for the
specification. You will need ordered fixtures when you have foreign key
constraints on keys in the same table. This is commonly needed for tree
structures. Example:
</p>
<pre>
   --- !omap
   - parent:
       id:         1
       parent_id:  NULL
       title:      Parent
   - child:
       id:         2
       parent_id:  1
       title:      Child
</pre>
<h1>CSV fixtures</h1>
<p>
<a href="Fixtures.html">Fixtures</a> can also be kept in the Comma
Separated Value format. Akin to YAML fixtures, CSV fixtures are stored in a
single file, but instead end with the <tt>.csv</tt> file extension (<a
href="Rails.html">Rails</a> example:
<tt>&lt;your-rails-app&gt;/test/fixtures/web_sites.csv</tt>).
</p>
<p>
The format of this type of fixture file is much more compact than the
others, but also a little harder to read by us humans. The first line of
the CSV file is a comma-separated list of field names. The rest of the file
is then comprised of the actual data (1 per line). Here&#8216;s an example:
</p>
<pre>
  id, name, url
  1, Ruby On Rails, http://www.rubyonrails.org
  2, Google, http://www.google.com
</pre>
<p>
Should you have a piece of data with a comma character in it, you can place
double quotes around that value. If you need to use a double quote
character, you must escape it with another double quote.
</p>
<p>
Another unique attribute of the CSV fixture is that it has <b>no</b>
fixture name like the other two formats. Instead, the fixture names are
automatically generated by deriving the class name of the fixture file and
adding an incrementing number to the end. In our example, the 1st fixture
would be called &quot;web_site_1&quot; and the 2nd one would be called
&quot;web_site_2&quot;.
</p>
<p>
Most databases and spreadsheets support exporting to CSV format, so this is
a great format for you to choose if you have existing data somewhere
already.
</p>
<h1>Single-file fixtures</h1>
<p>
This type of fixture was the original format for Active Record that has
since been deprecated in favor of the YAML and CSV formats. <a
href="Fixtures.html">Fixtures</a> for this format are created by placing
text files in a sub-directory (with the name of the model) to the directory
appointed by <tt>ActiveSupport::TestCase.fixture_path=(path)</tt> (this is
automatically configured for <a href="Rails.html">Rails</a>, so you can
just put your files in
<tt>&lt;your-rails-app&gt;/test/fixtures/&lt;your-model-name&gt;/</tt>
&#8212; like <tt>&lt;your-rails-app&gt;/test/fixtures/web_sites/</tt> for
the WebSite model).
</p>
<p>
Each text file placed in this directory represents a &quot;record&quot;.
Usually these types of fixtures are named without extensions, but if you
are on a Windows machine, you might consider adding <tt>.txt</tt> as the
extension. Here&#8216;s what the above example might look like:
</p>
<pre>
  web_sites/google
  web_sites/yahoo.txt
  web_sites/ruby-on-rails
</pre>
<p>
The file format of a standard fixture is simple. Each line is a property
(or column in db speak) and has the syntax of &quot;name =&gt; value&quot;.
Here&#8216;s an example of the ruby-on-rails fixture above:
</p>
<pre>
  id =&gt; 1
  name =&gt; Ruby on Rails
  url =&gt; http://www.rubyonrails.org
</pre>
<h1>Using <a href="Fixtures.html">Fixtures</a></h1>
<p>
Since fixtures are a testing construct, we use them in our unit and
functional tests. There are two ways to use the fixtures, but first
let&#8216;s take a look at a sample unit test:
</p>
<pre>
  require 'web_site'

  class WebSiteTest &lt; ActiveSupport::TestCase
    def test_web_site_count
      assert_equal 2, WebSite.count
    end
  end
</pre>
<p>
As it stands, unless we pre-load the web_site table in our database with
two records, this test will fail. Here&#8216;s the easiest way to add
fixtures to the database:
</p>
<pre>
  ...
  class WebSiteTest &lt; ActiveSupport::TestCase
    fixtures :web_sites # add more by separating the symbols with commas
  ...
</pre>
<p>
By adding a &quot;fixtures&quot; method to the test case and passing it a
list of symbols (only one is shown here though), we trigger the testing
environment to automatically load the appropriate fixtures into the
database before each test. To ensure consistent data, the environment
deletes the fixtures before running the load.
</p>
<p>
In addition to being available in the database, the fixtures are also
loaded into a hash stored in an instance variable of the test case. It is
named after the symbol&#8230; so, in our example, there would be a hash
available called <tt>@web_sites</tt>. This is where the &quot;fixture
name&quot; comes into play.
</p>
<p>
On top of that, each record is automatically &quot;found&quot; (using
<tt>Model.find(id)</tt>) and placed in the instance variable of its name.
So for the YAML fixtures, we&#8216;d get <tt>@rubyonrails</tt> and
<tt>@google</tt>, which could be interrogated using regular Active Record
semantics:
</p>
<pre>
  # test if the object created from the fixture data has the same attributes as the data itself
  def test_find
    assert_equal @web_sites[&quot;rubyonrails&quot;][&quot;name&quot;], @rubyonrails.name
  end
</pre>
<p>
As seen above, the data hash created from the YAML fixtures would have
<tt>@web_sites[&quot;rubyonrails&quot;][&quot;url&quot;]</tt> return
&quot;<a href="http://www.rubyonrails.org">www.rubyonrails.org</a>&quot;
and <tt>@web_sites[&quot;google&quot;][&quot;name&quot;]</tt> would return
&quot;Google&quot;. The same fixtures, but loaded from a CSV fixture file,
would be accessible via
<tt>@web_sites[&quot;web_site_1&quot;][&quot;name&quot;] == &quot;Ruby on
<a href="Rails.html">Rails</a>&quot;</tt> and have the individual fixtures
available as instance variables <tt>@web_site_1</tt> and
<tt>@web_site_2</tt>.
</p>
<p>
If you do not wish to use instantiated fixtures (usually for performance
reasons) there are two options.
</p>
<pre>
  - to completely disable instantiated fixtures:
      self.use_instantiated_fixtures = false

  - to keep the fixture instance (@web_sites) available, but do not automatically 'find' each instance:
      self.use_instantiated_fixtures = :no_instances
</pre>
<p>
Even if auto-instantiated fixtures are disabled, you can still access them
by name via special dynamic methods. Each method has the same name as the
model, and accepts the name of the fixture to instantiate:
</p>
<pre>
  fixtures :web_sites

  def test_find
    assert_equal &quot;Ruby on Rails&quot;, web_sites(:rubyonrails).name
  end
</pre>
<h1>Dynamic fixtures with ERb</h1>
<p>
Some times you don&#8216;t care about the content of the fixtures as much
as you care about the volume. In these cases, you can mix ERb in with your
YAML or CSV fixtures to create a bunch of fixtures for load testing, like:
</p>
<pre>
  &lt;% for i in 1..1000 %&gt;
  fix_&lt;%= i %&gt;:
    id: &lt;%= i %&gt;
    name: guy_&lt;%= 1 %&gt;
  &lt;% end %&gt;
</pre>
<p>
This will create 1000 very simple YAML fixtures.
</p>
<p>
Using ERb, you can also inject dynamic values into your fixtures with
inserts like <tt>&lt;%= Date.today.strftime(&quot;%Y-%m-%d&quot;)
%&gt;</tt>. This is however a feature to be used with some caution. The
point of fixtures are that they&#8216;re stable units of predictable sample
data. If you feel that you need to inject dynamic values, then perhaps you
should reexamine whether your application is properly testable. Hence,
dynamic values in fixtures are to be considered a code smell.
</p>
<h1>Transactional fixtures</h1>
<p>
TestCases can use begin+rollback to isolate their changes to the database
instead of having to delete+insert for every test case. They can also turn
off auto-instantiation of fixture data since the feature is costly and
often unused.
</p>
<pre>
  class FooTest &lt; ActiveSupport::TestCase
    self.use_transactional_fixtures = true
    self.use_instantiated_fixtures = false

    fixtures :foos

    def test_godzilla
      assert !Foo.find(:all).empty?
      Foo.destroy_all
      assert Foo.find(:all).empty?
    end

    def test_godzilla_aftermath
      assert !Foo.find(:all).empty?
    end
  end
</pre>
<p>
If you preload your test database with all fixture data (probably in the
Rakefile task) and use transactional fixtures, then you may omit all
fixtures declarations in your test cases since all the data&#8216;s already
there and every case rolls back its changes.
</p>
<p>
In order to use instantiated fixtures with preloaded data, set
+self.pre_loaded_fixtures+ to true. This will provide access to fixture
data for every table that has been loaded through fixtures (depending on
the value of <tt>use_instantiated_fixtures</tt>)
</p>
<p>
When <b>not</b> to use transactional fixtures:
</p>
<pre>
  1. You're testing whether a transaction works correctly. Nested transactions don't commit until all parent transactions commit,
     particularly, the fixtures transaction which is begun in setup and rolled back in teardown. Thus, you won't be able to verify
     the results of your transaction until Active Record supports nested transactions or savepoints (in progress).
  2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM.
     Use InnoDB, MaxDB, or NDB instead.
</pre>
<h1>Advanced YAML <a href="Fixtures.html">Fixtures</a></h1>
<p>
YAML fixtures that don&#8216;t specify an ID get some extra features:
</p>
<ul>
<li>Stable, autogenerated ID&#8216;s

</li>
<li>Label references for associations (belongs_to, has_one, has_many)

</li>
<li>HABTM associations as inline lists

</li>
<li>Autofilled timestamp columns

</li>
<li>Fixture label interpolation

</li>
<li>Support for YAML defaults

</li>
</ul>
<h2>Stable, autogenerated ID&#8216;s</h2>
<p>
Here, have a monkey fixture:
</p>
<pre>
  george:
    id: 1
    name: George the Monkey

  reginald:
    id: 2
    name: Reginald the Pirate
</pre>
<p>
Each of these fixtures has two unique identifiers: one for the database and
one for the humans. Why don&#8216;t we generate the primary key instead?
Hashing each fixture&#8216;s label yields a consistent ID:
</p>
<pre>
  george: # generated id: 503576764
    name: George the Monkey

  reginald: # generated id: 324201669
    name: Reginald the Pirate
</pre>
<p>
Active Record looks at the fixture&#8216;s model class, discovers the
correct primary key, and generates it right before inserting the fixture
into the database.
</p>
<p>
The generated ID for a given label is constant, so we can discover any
fixture&#8216;s ID without loading anything, as long as we know the label.
</p>
<h2>Label references for associations (belongs_to, has_one, has_many)</h2>
<p>
Specifying foreign keys in fixtures can be very fragile, not to mention
difficult to read. Since Active Record can figure out the ID of any fixture
from its label, you can specify FK&#8216;s by label instead of ID.
</p>
<h3>belongs_to</h3>
<p>
Let&#8216;s break out some more monkeys and pirates.
</p>
<pre>
  ### in pirates.yml

  reginald:
    id: 1
    name: Reginald the Pirate
    monkey_id: 1

  ### in monkeys.yml

  george:
    id: 1
    name: George the Monkey
    pirate_id: 1
</pre>
<p>
Add a few more monkeys and pirates and break this into multiple files, and
it gets pretty hard to keep track of what&#8216;s going on. Let&#8216;s use
labels instead of ID&#8216;s:
</p>
<pre>
  ### in pirates.yml

  reginald:
    name: Reginald the Pirate
    monkey: george

  ### in monkeys.yml

  george:
    name: George the Monkey
    pirate: reginald
</pre>
<p>
Pow! All is made clear. Active Record reflects on the fixture&#8216;s model
class, finds all the <tt>belongs_to</tt> associations, and allows you to
specify a target <b>label</b> for the <b>association</b> (monkey: george)
rather than a target <b>id</b> for the <b>FK</b> (<tt>monkey_id: 1</tt>).
</p>
<h4>Polymorphic belongs_to</h4>
<p>
Supporting polymorphic relationships is a little bit more complicated,
since Active Record needs to know what type your association is pointing
at. Something like this should look familiar:
</p>
<pre>
  ### in fruit.rb

  belongs_to :eater, :polymorphic =&gt; true

  ### in fruits.yml

  apple:
    id: 1
    name: apple
    eater_id: 1
    eater_type: Monkey
</pre>
<p>
Can we do better? You bet!
</p>
<pre>
  apple:
    eater: george (Monkey)
</pre>
<p>
Just provide the polymorphic target type and Active Record will take care
of the rest.
</p>
<h3>has_and_belongs_to_many</h3>
<p>
Time to give our monkey some fruit.
</p>
<pre>
  ### in monkeys.yml

  george:
    id: 1
    name: George the Monkey
    pirate_id: 1

  ### in fruits.yml

  apple:
    id: 1
    name: apple

  orange:
    id: 2
    name: orange

  grape:
    id: 3
    name: grape

  ### in fruits_monkeys.yml

  apple_george:
    fruit_id: 1
    monkey_id: 1

  orange_george:
    fruit_id: 2
    monkey_id: 1

  grape_george:
    fruit_id: 3
    monkey_id: 1
</pre>
<p>
Let&#8216;s make the HABTM fixture go away.
</p>
<pre>
  ### in monkeys.yml

  george:
    name: George the Monkey
    pirate: reginald
    fruits: apple, orange, grape

  ### in fruits.yml

  apple:
    name: apple

  orange:
    name: orange

  grape:
    name: grape
</pre>
<p>
Zap! No more fruits_monkeys.yml file. We&#8216;ve specified the list of
fruits on George&#8216;s fixture, but we could&#8216;ve just as easily
specified a list of monkeys on each fruit. As with <tt>belongs_to</tt>,
Active Record reflects on the fixture&#8216;s model class and discovers the
<tt>has_and_belongs_to_many</tt> associations.
</p>
<h2>Autofilled timestamp columns</h2>
<p>
If your table/model specifies any of Active Record&#8216;s standard
timestamp columns (<tt>created_at</tt>, <tt>created_on</tt>,
<tt>updated_at</tt>, <tt>updated_on</tt>), they will automatically be set
to <tt>Time.now</tt>.
</p>
<p>
If you&#8216;ve set specific values, they&#8216;ll be left alone.
</p>
<h2>Fixture label interpolation</h2>
<p>
The label of the current fixture is always available as a column value:
</p>
<pre>
  geeksomnia:
    name: Geeksomnia's Account
    subdomain: $LABEL
</pre>
<p>
Also, sometimes (like when porting older join table fixtures) you&#8216;ll
need to be able to get ahold of the identifier for a given label. <a
href="ERB.html">ERB</a> to the rescue:
</p>
<pre>
  george_reginald:
    monkey_id: &lt;%= Fixtures.identify(:reginald) %&gt;
    pirate_id: &lt;%= Fixtures.identify(:george) %&gt;
</pre>
<h2>Support for YAML defaults</h2>
<p>
You probably already know how to use YAML to set and reuse defaults in your
<tt>database.yml</tt> file. You can use the same technique in your
fixtures:
</p>
<pre>
  DEFAULTS: &amp;DEFAULTS
    created_on: &lt;%= 3.weeks.ago.to_s(:db) %&gt;

  first:
    name: Smurf
    &lt;&lt;: *DEFAULTS

  second:
    name: Fraggle
    &lt;&lt;: *DEFAULTS
</pre>
<p>
Any fixture labeled &quot;DEFAULTS&quot; is safely ignored.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000184">cache_fixtures</a>&nbsp;&nbsp;
      <a href="#M000181">cache_for_connection</a>&nbsp;&nbsp;
      <a href="#M000183">cached_fixtures</a>&nbsp;&nbsp;
      <a href="#M000187">create_fixtures</a>&nbsp;&nbsp;
      <a href="#M000190">delete_existing_fixtures</a>&nbsp;&nbsp;
      <a href="#M000182">fixture_is_cached?</a>&nbsp;&nbsp;
      <a href="#M000188">identify</a>&nbsp;&nbsp;
      <a href="#M000191">insert_fixtures</a>&nbsp;&nbsp;
      <a href="#M000186">instantiate_all_loaded_fixtures</a>&nbsp;&nbsp;
      <a href="#M000185">instantiate_fixtures</a>&nbsp;&nbsp;
      <a href="#M000189">new</a>&nbsp;&nbsp;
      <a href="#M000180">reset_cache</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">


    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">DEFAULT_FILTER_RE</td>
          <td>=</td>
          <td class="context-item-value">/\.ya?ml$/</td>
        </tr>
        </table>
      </div>
    </div>



    <div id="attribute-list">
      <h3 class="section-bar">Attributes</h3>

      <div class="name-list">
        <table>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">name</td>
          <td class="context-item-value">&nbsp;[R]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">table_name</td>
          <td class="context-item-value">&nbsp;[R]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        </table>
      </div>
    </div>
      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000184" class="method-detail">
        <a name="M000184"></a>

        <div class="method-heading">
          <a href="#M000184" class="method-signature">
          <span class="method-name">cache_fixtures</span><span class="method-args">(connection, fixtures_map)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000184-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000184-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 473</span>
473:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">cache_fixtures</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-identifier">fixtures_map</span>)
474:     <span class="ruby-identifier">cache_for_connection</span>(<span class="ruby-identifier">connection</span>).<span class="ruby-identifier">update</span>(<span class="ruby-identifier">fixtures_map</span>)
475:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000181" class="method-detail">
        <a name="M000181"></a>

        <div class="method-heading">
          <a href="#M000181" class="method-signature">
          <span class="method-name">cache_for_connection</span><span class="method-args">(connection)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000181-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000181-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 455</span>
455:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">cache_for_connection</span>(<span class="ruby-identifier">connection</span>)
456:     <span class="ruby-ivar">@@all_cached_fixtures</span>[<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">object_id</span>] <span class="ruby-operator">||=</span> {}
457:     <span class="ruby-ivar">@@all_cached_fixtures</span>[<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">object_id</span>]
458:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000183" class="method-detail">
        <a name="M000183"></a>

        <div class="method-heading">
          <a href="#M000183" class="method-signature">
          <span class="method-name">cached_fixtures</span><span class="method-args">(connection, keys_to_fetch = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000183-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000183-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 464</span>
464:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">cached_fixtures</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-identifier">keys_to_fetch</span> = <span class="ruby-keyword kw">nil</span>)
465:     <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">keys_to_fetch</span>
466:       <span class="ruby-identifier">fixtures</span> = <span class="ruby-identifier">cache_for_connection</span>(<span class="ruby-identifier">connection</span>).<span class="ruby-identifier">values_at</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">keys_to_fetch</span>)
467:     <span class="ruby-keyword kw">else</span>
468:       <span class="ruby-identifier">fixtures</span> = <span class="ruby-identifier">cache_for_connection</span>(<span class="ruby-identifier">connection</span>).<span class="ruby-identifier">values</span>
469:     <span class="ruby-keyword kw">end</span>
470:     <span class="ruby-identifier">fixtures</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">fixtures</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">fixtures</span>.<span class="ruby-identifier">first</span>
471:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000187" class="method-detail">
        <a name="M000187"></a>

        <div class="method-heading">
          <a href="#M000187" class="method-signature">
          <span class="method-name">create_fixtures</span><span class="method-args">(fixtures_directory, table_names, class_names = {}) {|: ActiveRecord::Base.connection| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000187-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000187-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 501</span>
501:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">create_fixtures</span>(<span class="ruby-identifier">fixtures_directory</span>, <span class="ruby-identifier">table_names</span>, <span class="ruby-identifier">class_names</span> = {})
502:     <span class="ruby-identifier">table_names</span> = [<span class="ruby-identifier">table_names</span>].<span class="ruby-identifier">flatten</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">n</span>.<span class="ruby-identifier">to_s</span> }
503:     <span class="ruby-identifier">connection</span>  = <span class="ruby-identifier">block_given?</span> <span class="ruby-value">? </span><span class="ruby-keyword kw">yield</span> <span class="ruby-operator">:</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">connection</span>
504: 
505:     <span class="ruby-identifier">table_names_to_fetch</span> = <span class="ruby-identifier">table_names</span>.<span class="ruby-identifier">reject</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">table_name</span><span class="ruby-operator">|</span> <span class="ruby-identifier">fixture_is_cached?</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-identifier">table_name</span>) }
506: 
507:     <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">table_names_to_fetch</span>.<span class="ruby-identifier">empty?</span>
508:       <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">silence</span> <span class="ruby-keyword kw">do</span>
509:         <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">disable_referential_integrity</span> <span class="ruby-keyword kw">do</span>
510:           <span class="ruby-identifier">fixtures_map</span> = {}
511: 
512:           <span class="ruby-identifier">fixtures</span> = <span class="ruby-identifier">table_names_to_fetch</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">table_name</span><span class="ruby-operator">|</span>
513:             <span class="ruby-identifier">fixtures_map</span>[<span class="ruby-identifier">table_name</span>] = <span class="ruby-constant">Fixtures</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-constant">File</span>.<span class="ruby-identifier">split</span>(<span class="ruby-identifier">table_name</span>.<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">last</span>, <span class="ruby-identifier">class_names</span>[<span class="ruby-identifier">table_name</span>.<span class="ruby-identifier">to_sym</span>], <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-identifier">fixtures_directory</span>, <span class="ruby-identifier">table_name</span>.<span class="ruby-identifier">to_s</span>))
514:           <span class="ruby-keyword kw">end</span>
515: 
516:           <span class="ruby-identifier">all_loaded_fixtures</span>.<span class="ruby-identifier">update</span>(<span class="ruby-identifier">fixtures_map</span>)
517: 
518:           <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">transaction</span>(<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">open_transactions</span>.<span class="ruby-identifier">zero?</span>) <span class="ruby-keyword kw">do</span>
519:             <span class="ruby-identifier">fixtures</span>.<span class="ruby-identifier">reverse</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">fixture</span><span class="ruby-operator">|</span> <span class="ruby-identifier">fixture</span>.<span class="ruby-identifier">delete_existing_fixtures</span> }
520:             <span class="ruby-identifier">fixtures</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">fixture</span><span class="ruby-operator">|</span> <span class="ruby-identifier">fixture</span>.<span class="ruby-identifier">insert_fixtures</span> }
521: 
522:             <span class="ruby-comment cmt"># Cap primary key sequences to max(pk).</span>
523:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:reset_pk_sequence!</span>)
524:               <span class="ruby-identifier">table_names</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">table_name</span><span class="ruby-operator">|</span>
525:                 <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">reset_pk_sequence!</span>(<span class="ruby-identifier">table_name</span>)
526:               <span class="ruby-keyword kw">end</span>
527:             <span class="ruby-keyword kw">end</span>
528:           <span class="ruby-keyword kw">end</span>
529: 
530:           <span class="ruby-identifier">cache_fixtures</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-identifier">fixtures_map</span>)
531:         <span class="ruby-keyword kw">end</span>
532:       <span class="ruby-keyword kw">end</span>
533:     <span class="ruby-keyword kw">end</span>
534:     <span class="ruby-identifier">cached_fixtures</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-identifier">table_names</span>)
535:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000182" class="method-detail">
        <a name="M000182"></a>

        <div class="method-heading">
          <a href="#M000182" class="method-signature">
          <span class="method-name">fixture_is_cached?</span><span class="method-args">(connection, table_name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000182-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000182-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 460</span>
460:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">fixture_is_cached?</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-identifier">table_name</span>)
461:     <span class="ruby-identifier">cache_for_connection</span>(<span class="ruby-identifier">connection</span>)[<span class="ruby-identifier">table_name</span>]
462:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000188" class="method-detail">
        <a name="M000188"></a>

        <div class="method-heading">
          <a href="#M000188" class="method-signature">
          <span class="method-name">identify</span><span class="method-args">(label)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a consistent identifier for <tt>label</tt>. This will always be a
positive integer, and will always be the same for a given label, assuming
the same OS, platform, and version of Ruby.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000188-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000188-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 540</span>
540:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">identify</span>(<span class="ruby-identifier">label</span>)
541:     <span class="ruby-identifier">label</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">hash</span>.<span class="ruby-identifier">abs</span>
542:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000186" class="method-detail">
        <a name="M000186"></a>

        <div class="method-heading">
          <a href="#M000186" class="method-signature">
          <span class="method-name">instantiate_all_loaded_fixtures</span><span class="method-args">(object, load_instances = true)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000186-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000186-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 492</span>
492:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">instantiate_all_loaded_fixtures</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">load_instances</span> = <span class="ruby-keyword kw">true</span>)
493:     <span class="ruby-identifier">all_loaded_fixtures</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">table_name</span>, <span class="ruby-identifier">fixtures</span><span class="ruby-operator">|</span>
494:       <span class="ruby-constant">Fixtures</span>.<span class="ruby-identifier">instantiate_fixtures</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">table_name</span>, <span class="ruby-identifier">fixtures</span>, <span class="ruby-identifier">load_instances</span>)
495:     <span class="ruby-keyword kw">end</span>
496:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000185" class="method-detail">
        <a name="M000185"></a>

        <div class="method-heading">
          <a href="#M000185" class="method-signature">
          <span class="method-name">instantiate_fixtures</span><span class="method-args">(object, table_name, fixtures, load_instances = true)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000185-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000185-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 477</span>
477:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">instantiate_fixtures</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">table_name</span>, <span class="ruby-identifier">fixtures</span>, <span class="ruby-identifier">load_instances</span> = <span class="ruby-keyword kw">true</span>)
478:     <span class="ruby-identifier">object</span>.<span class="ruby-identifier">instance_variable_set</span> <span class="ruby-node">&quot;@#{table_name.to_s.gsub('.','_')}&quot;</span>, <span class="ruby-identifier">fixtures</span>
479:     <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">load_instances</span>
480:       <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">silence</span> <span class="ruby-keyword kw">do</span>
481:         <span class="ruby-identifier">fixtures</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span>, <span class="ruby-identifier">fixture</span><span class="ruby-operator">|</span>
482:           <span class="ruby-keyword kw">begin</span>
483:             <span class="ruby-identifier">object</span>.<span class="ruby-identifier">instance_variable_set</span> <span class="ruby-node">&quot;@#{name}&quot;</span>, <span class="ruby-identifier">fixture</span>.<span class="ruby-identifier">find</span>
484:           <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">FixtureClassNotFound</span>
485:             <span class="ruby-keyword kw">nil</span>
486:           <span class="ruby-keyword kw">end</span>
487:         <span class="ruby-keyword kw">end</span>
488:       <span class="ruby-keyword kw">end</span>
489:     <span class="ruby-keyword kw">end</span>
490:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000189" class="method-detail">
        <a name="M000189"></a>

        <div class="method-heading">
          <a href="#M000189" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000189-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000189-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 546</span>
546:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">connection</span>, <span class="ruby-identifier">table_name</span>, <span class="ruby-identifier">class_name</span>, <span class="ruby-identifier">fixture_path</span>, <span class="ruby-identifier">file_filter</span> = <span class="ruby-constant">DEFAULT_FILTER_RE</span>)
547:     <span class="ruby-ivar">@connection</span>, <span class="ruby-ivar">@table_name</span>, <span class="ruby-ivar">@fixture_path</span>, <span class="ruby-ivar">@file_filter</span> = <span class="ruby-identifier">connection</span>, <span class="ruby-identifier">table_name</span>, <span class="ruby-identifier">fixture_path</span>, <span class="ruby-identifier">file_filter</span>
548:     <span class="ruby-ivar">@name</span> = <span class="ruby-identifier">table_name</span> <span class="ruby-comment cmt"># preserve fixture base name</span>
549:     <span class="ruby-ivar">@class_name</span> = <span class="ruby-identifier">class_name</span> <span class="ruby-operator">||</span>
550:                   (<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">pluralize_table_names</span> <span class="ruby-value">? </span><span class="ruby-ivar">@table_name</span>.<span class="ruby-identifier">singularize</span>.<span class="ruby-identifier">camelize</span> <span class="ruby-operator">:</span> <span class="ruby-ivar">@table_name</span>.<span class="ruby-identifier">camelize</span>)
551:     <span class="ruby-ivar">@table_name</span> = <span class="ruby-node">&quot;#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}&quot;</span>
552:     <span class="ruby-ivar">@table_name</span> = <span class="ruby-identifier">class_name</span>.<span class="ruby-identifier">table_name</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">class_name</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:table_name</span>)
553:     <span class="ruby-ivar">@connection</span> = <span class="ruby-identifier">class_name</span>.<span class="ruby-identifier">connection</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">class_name</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:connection</span>)
554:     <span class="ruby-identifier">read_fixture_files</span>
555:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000180" class="method-detail">
        <a name="M000180"></a>

        <div class="method-heading">
          <a href="#M000180" class="method-signature">
          <span class="method-name">reset_cache</span><span class="method-args">(connection = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000180-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000180-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 450</span>
450:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">reset_cache</span>(<span class="ruby-identifier">connection</span> = <span class="ruby-keyword kw">nil</span>)
451:     <span class="ruby-identifier">connection</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">connection</span>
452:     <span class="ruby-ivar">@@all_cached_fixtures</span>[<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">object_id</span>] = {}
453:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000190" class="method-detail">
        <a name="M000190"></a>

        <div class="method-heading">
          <a href="#M000190" class="method-signature">
          <span class="method-name">delete_existing_fixtures</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000190-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000190-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 557</span>
557:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">delete_existing_fixtures</span>
558:     <span class="ruby-ivar">@connection</span>.<span class="ruby-identifier">delete</span> <span class="ruby-node">&quot;DELETE FROM #{@connection.quote_table_name(table_name)}&quot;</span>, <span class="ruby-value str">'Fixture Delete'</span>
559:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000191" class="method-detail">
        <a name="M000191"></a>

        <div class="method-heading">
          <a href="#M000191" class="method-signature">
          <span class="method-name">insert_fixtures</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000191-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000191-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/fixtures.rb, line 561</span>
561:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">insert_fixtures</span>
562:     <span class="ruby-identifier">now</span> = <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">default_timezone</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">:utc</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">utc</span> <span class="ruby-operator">:</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
563:     <span class="ruby-identifier">now</span> = <span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-identifier">:db</span>)
564: 
565:     <span class="ruby-comment cmt"># allow a standard key to be used for doing defaults in YAML</span>
566:     <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
567:       <span class="ruby-identifier">delete</span>(<span class="ruby-value str">'DEFAULTS'</span>)
568:     <span class="ruby-keyword kw">else</span>
569:       <span class="ruby-identifier">delete</span>(<span class="ruby-identifier">assoc</span>(<span class="ruby-value str">'DEFAULTS'</span>))
570:     <span class="ruby-keyword kw">end</span>
571: 
572:     <span class="ruby-comment cmt"># track any join tables we need to insert later</span>
573:     <span class="ruby-identifier">habtm_fixtures</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">h</span>, <span class="ruby-identifier">habtm</span><span class="ruby-operator">|</span>
574:       <span class="ruby-identifier">h</span>[<span class="ruby-identifier">habtm</span>] = <span class="ruby-constant">HabtmFixtures</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@connection</span>, <span class="ruby-identifier">habtm</span>.<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:join_table</span>], <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>)
575:     <span class="ruby-keyword kw">end</span>
576: 
577:     <span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">label</span>, <span class="ruby-identifier">fixture</span><span class="ruby-operator">|</span>
578:       <span class="ruby-identifier">row</span> = <span class="ruby-identifier">fixture</span>.<span class="ruby-identifier">to_hash</span>
579: 
580:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">model_class</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">model_class</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
581:         <span class="ruby-comment cmt"># fill in timestamp columns if they aren't specified and the model is set to record_timestamps</span>
582:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">model_class</span>.<span class="ruby-identifier">record_timestamps</span>
583:           <span class="ruby-identifier">timestamp_column_names</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span><span class="ruby-operator">|</span>
584:             <span class="ruby-identifier">row</span>[<span class="ruby-identifier">name</span>] = <span class="ruby-identifier">now</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">name</span>)
585:           <span class="ruby-keyword kw">end</span>
586:         <span class="ruby-keyword kw">end</span>
587: 
588:         <span class="ruby-comment cmt"># interpolate the fixture label</span>
589:         <span class="ruby-identifier">row</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
590:           <span class="ruby-identifier">row</span>[<span class="ruby-identifier">key</span>] = <span class="ruby-identifier">label</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;$LABEL&quot;</span>
591:         <span class="ruby-keyword kw">end</span>
592: 
593:         <span class="ruby-comment cmt"># generate a primary key if necessary</span>
594:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">has_primary_key_column?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">row</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">primary_key_name</span>)
595:           <span class="ruby-identifier">row</span>[<span class="ruby-identifier">primary_key_name</span>] = <span class="ruby-constant">Fixtures</span>.<span class="ruby-identifier">identify</span>(<span class="ruby-identifier">label</span>)
596:         <span class="ruby-keyword kw">end</span>
597: 
598:         <span class="ruby-comment cmt"># If STI is used, find the correct subclass for association reflection</span>
599:         <span class="ruby-identifier">reflection_class</span> =
600:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">inheritance_column_name</span>)
601:             <span class="ruby-identifier">row</span>[<span class="ruby-identifier">inheritance_column_name</span>].<span class="ruby-identifier">constantize</span> <span class="ruby-keyword kw">rescue</span> <span class="ruby-identifier">model_class</span>
602:           <span class="ruby-keyword kw">else</span>
603:             <span class="ruby-identifier">model_class</span>
604:           <span class="ruby-keyword kw">end</span>
605: 
606:         <span class="ruby-identifier">reflection_class</span>.<span class="ruby-identifier">reflect_on_all_associations</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">association</span><span class="ruby-operator">|</span>
607:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">association</span>.<span class="ruby-identifier">macro</span>
608:           <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:belongs_to</span>
609:             <span class="ruby-comment cmt"># Do not replace association name with association foreign key if they are named the same</span>
610:             <span class="ruby-identifier">fk_name</span> = (<span class="ruby-identifier">association</span>.<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:foreign_key</span>] <span class="ruby-operator">||</span> <span class="ruby-node">&quot;#{association.name}_id&quot;</span>).<span class="ruby-identifier">to_s</span>
611: 
612:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">association</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">fk_name</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">value</span> = <span class="ruby-identifier">row</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">association</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>)
613:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">association</span>.<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:polymorphic</span>]
614:                 <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-regexp re">/\s*\(([^\)]*)\)\s*$/</span>, <span class="ruby-value str">&quot;&quot;</span>)
615:                   <span class="ruby-identifier">target_type</span> = <span class="ruby-identifier">$1</span>
616:                   <span class="ruby-identifier">target_type_name</span> = (<span class="ruby-identifier">association</span>.<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:foreign_type</span>] <span class="ruby-operator">||</span> <span class="ruby-node">&quot;#{association.name}_type&quot;</span>).<span class="ruby-identifier">to_s</span>
617: 
618:                   <span class="ruby-comment cmt"># support polymorphic belongs_to as &quot;label (Type)&quot;</span>
619:                   <span class="ruby-identifier">row</span>[<span class="ruby-identifier">target_type_name</span>] = <span class="ruby-identifier">target_type</span>
620:                 <span class="ruby-keyword kw">end</span>
621:               <span class="ruby-keyword kw">end</span>
622: 
623:               <span class="ruby-identifier">row</span>[<span class="ruby-identifier">fk_name</span>] = <span class="ruby-constant">Fixtures</span>.<span class="ruby-identifier">identify</span>(<span class="ruby-identifier">value</span>)
624:             <span class="ruby-keyword kw">end</span>
625:           <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:has_and_belongs_to_many</span>
626:             <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">targets</span> = <span class="ruby-identifier">row</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">association</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>))
627:               <span class="ruby-identifier">targets</span> = <span class="ruby-identifier">targets</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">targets</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">targets</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp re">/\s*,\s*/</span>)
628:               <span class="ruby-identifier">join_fixtures</span> = <span class="ruby-identifier">habtm_fixtures</span>[<span class="ruby-identifier">association</span>]
629: 
630:               <span class="ruby-identifier">targets</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">target</span><span class="ruby-operator">|</span>
631:                 <span class="ruby-identifier">join_fixtures</span>[<span class="ruby-node">&quot;#{label}_#{target}&quot;</span>] = <span class="ruby-constant">Fixture</span>.<span class="ruby-identifier">new</span>(
632:                   { <span class="ruby-identifier">association</span>.<span class="ruby-identifier">primary_key_name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">row</span>[<span class="ruby-identifier">primary_key_name</span>],
633:                     <span class="ruby-identifier">association</span>.<span class="ruby-identifier">association_foreign_key</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">Fixtures</span>.<span class="ruby-identifier">identify</span>(<span class="ruby-identifier">target</span>) }, <span class="ruby-keyword kw">nil</span>)
634:               <span class="ruby-keyword kw">end</span>
635:             <span class="ruby-keyword kw">end</span>
636:           <span class="ruby-keyword kw">end</span>
637:         <span class="ruby-keyword kw">end</span>
638:       <span class="ruby-keyword kw">end</span>
639: 
640:       <span class="ruby-ivar">@connection</span>.<span class="ruby-identifier">insert_fixture</span>(<span class="ruby-identifier">fixture</span>, <span class="ruby-ivar">@table_name</span>)
641:     <span class="ruby-keyword kw">end</span>
642: 
643:     <span class="ruby-comment cmt"># insert any HABTM join tables we discovered</span>
644:     <span class="ruby-identifier">habtm_fixtures</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">fixture</span><span class="ruby-operator">|</span>
645:       <span class="ruby-identifier">fixture</span>.<span class="ruby-identifier">delete_existing_fixtures</span>
646:       <span class="ruby-identifier">fixture</span>.<span class="ruby-identifier">insert_fixtures</span>
647:     <span class="ruby-keyword kw">end</span>
648:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>